#!/bin/echo this scriptlet should be sourced by a build recipe

# RECIPE START common
# RECIPE SKIP

TARGET=loongarch64-linux-gnu
LINUX_ARCH=loongarch
LINUX_DEFCONFIG=loongson3_defconfig
LINUX_SUBREPO=loongarch-next
GRUB_ARCH=loongarch64
target_libdir=lib64
prefix=/usr
sysconfdir='/etc'

# The name of bootloader EFI binary in EFI/$bootloader_id
GRUB_EFI_FILENAME='grubloongarch64.efi'
# The name of bootloader EFI binary in EFI/BOOT
ARCH_EFI_REMOVABLE_NAMES='BOOTLOONGARCH64.EFI BOOTLOONGARCH.EFI'
# GPT UUID of root partition, per discoverable partitions specification
ARCH_ROOTFS_UUID='77055800-792c-4f94-b39a-98c91b762bb6'


RU_DNS_SERVERS='195.208.4.1#a.res-nsdi.ru 195.208.5.1#b.res-nsdi.ru 2a0c:a9c7:8::1#a.res-nsdi.ru 2a0c:a9c7:9::1#b.res-nsdi.ru'

BUILD=`uname -m`-linux-gnu
stage="`pwd`/stage/$TARGET"
# Native compiler and glibc goes here
rootfs="`pwd`/rootfs/$TARGET"
# A complete bootable rootfs is here
r00tfs="`pwd`/r00tfs/$TARGET"
xtools="`pwd`/xtools"
build_prefix="/tools"
CFLAGS="-O2 -g -I${stage}${prefix}/include"
export CFLAGS
CPPFLAGS="-I${stage}${prefix}/include"
export CPPFLAGS
LDFLAGS="-L${stage}${prefix}/${target_libdir} -Wl,-rpath-link -Wl,${stage}${prefix}/${target_libdir}"
export LDFLAGS
CONFIGURE_ARGS="--host=$TARGET --build=$BUILD --prefix=$prefix --libdir=$prefix/${target_libdir} --localstatedir=/var/lib --sysconfdir=${sysconfdir}"
AUTOCRAP_KLUDGE="ACLOCAL=':' AUTOCONF=':' AUTOHEADER=':' AUTOMAKE=':'"
NATIVE_CONFIGURE_ARGS="--host=$BUILD --build=$BUILD --prefix=$build_prefix --sbindir=$build_prefix/bin"

export PATH="${xtools}${build_prefix}/bin:${PATH}"

JOBS=$(nproc)

# find out the total number of cores available in distcc cluster
distcc_total_cores () {
	local dcc_hosts="$1"
	local total_cores=0
	local cores=0

	if [ -z "$dcc_hosts" ]; then
		dcc_hosts="$HOME/.distcc/hosts"
	fi

	if [ ! -e "$dcc_hosts" ]; then
		echo 0
		return
	fi

	while read line; do
		case "$line" in
		--*)
			;; # ignore
		'#'*)
			;; # ignore
		[0-9]*)
			cores="${line##*/}"
			cores="${cores%%,*}"
			total_cores=$((total_cores+cores))
			;;
		esac
	done < "$dcc_hosts"
	echo "$total_cores"
}

if [ -d "/usr/lib/ccache" ]; then
	export PATH=/usr/lib/ccache:$PATH
	if [ -x /usr/local/bin/distcc ]; then
		_distcc=/usr/local/bin/distcc
	elif [ -x /usr/bin/distcc ]; then
		_distcc=/usr/bin/distcc
	fi
	dcc_cores=0
	if [ -n "$_distcc" ]; then
		dcc_cores=`distcc_total_cores`
	fi
	if [ "$dcc_cores" != '0' ]; then
		export CCACHE_PREFIX="$_distcc"
		export DISTCC_BACKOFF_PERIOD=0
		export DISTCC_FALLBACK=0
		JOBS="${dcc_cores}"
		printf "Enabled distcc, total %d cores\n" $JOBS
	fi
fi

# XXX:
# git does not preserve the modification time for a good reason.
# Consider building some old version like this
#
# git checkout $SOME_OLD_COMMIT_SHA1
# make
# 
# If git preserved mtime make won't rebuild anything for current
# sources are (way) older than the corresponding object files.
# Hence git updates mtimes on every checkout.
# However this triggers spurious rebuilds with autotools, and
# those rebuilds typically require extra tools (and fail if
# these tools are not available or "wrong" version is installed)
# To avoid the problem reset mtime of all source tree to that
# of some reference file
reset_mtime() {
	local dir="$1"
	local ref="$2"

	find "$dir" -type f -print0 | xargs -0 -r touch -c -m --reference="$ref"
}

wipe_la_files() {
	local dir="$1"
	find "$dir" -type f -name '*.la' -delete
}

make_build() {
	local dir="$1"
	make -j${JOBS} -C "$dir"
	make -j${JOBS} -C "$dir" install DESTDIR="$stage"
	wipe_la_files "$stage"
	rm -rf "$dir"
}

make_native_build() {
	local dir="$1"
	make -j${JOBS} -C "$dir"
	make -j${JOBS} -C "$dir" install DESTDIR="$xtools"
	wipe_la_files "$xtools"
	rm -rf "$dir"
}

relocate_to_usr() {
	local root="$1"
	for thedir in "$target_libdir" lib bin sbin; do
		if [ -L "$root/$thedir" ]; then
			# relax, it's already a symlink
			:
		elif [ -d "$root/$thedir" ]; then
			rsync -avcH "$root/$thedir/" "${root}${prefix}/$thedir/"
			rm -rf "$root/$thedir"
			ln -s "${prefix#/}/$thedir" "${root}/$thedir"
		else
			ln -s "${prefix#/}/$thedir" "${root}/$thedir"
		fi
	done
}

# RECIPE END common
